#include<bits/stdc++.h>
using namespace std;
#define int long long

struct node {
	int x, y, val;
};

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int a[105][105], ans[105][105];

signed main() {
	int n, m;
	cin >> n >> m;

	vector<node> v;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			v.push_back({i, j, a[i][j]});
		}
	}

	sort(v.begin(), v.end(), [](node x, node y) {
		return x.val < y.val;
	});

//	for (auto ii : v) {
//		if (ii.x > 1 && a[ii.x - 1][ii.y] > a[ii.x][ii.y])
//			ans[ii.x - 1][ii.y] = ans[ii.x][ii.y] + 1;
//
//		if (ii.x < n && a[ii.x + 1][ii.y] > a[ii.x][ii.y])
//			ans[ii.x + 1][ii.y] = ans[ii.x][ii.y] + 1;
//
//		if (ii.y > 1 && a[ii.x][ii.y - 1] > a[ii.x][ii.y])
//			ans[ii.x][ii.y - 1] = ans[ii.x][ii.y] + 1;
//
//		if (ii.y < m && a[ii.x][ii.y + 1] > a[ii.x][ii.y])
//			ans[ii.x][ii.y + 1] = ans[ii.x][ii.y] + 1;
//	}

	ans[v[0].x][v[0].y] = 1;
	for (int i = 0; i < v.size(); i++) {
		int x = v[i].x;
		int y = v[i].y;
		for (int k = 0; k < 4; k++) {
			int xx = x + dx[k];
			int yy = y + dy[k];

			if (xx < 1 || xx > n || yy < 1 || yy > m)
				continue;
			if (a[xx][yy] > v[i].val) {
				ans[xx][yy] = ans[x][y] + 1;
//				ans[xx][yy] = max(ans[xx][yy], ans[x][y] + 1);
			}
		}
	}
	cout << *max_element(&ans[1][1], &ans[n + 1][m + 1]) + 1;
	return 0;
}
